<template>
	<view class="login">
		<view class="login_title">
			<text>手机号码登录</text>
		</view>
		<view class="login_user" :class="nameFocus ?  'nameFocus' : ''">
			<input type="text" placeholder="手机号码" placeholder-style="color:rgb(173,185,193)" v-model="name" @focus="onFocusName" @blur="onBlurName"/>
		</view>
		<view class="login_pwd" :class="psdFocus ? 'psdFocus' : ''">
			<input type="text" password placeholder="用户密码" placeholder-style="color:rgb(173,185,193)" v-model="psd" @focus="onFocusPsd" @blur="onBlurPsd" />
		</view>
		<view class="login_btn">
			<button hover-class="btn_hover" @tap="login">登录</button>
		</view>

		<view class="login_text">
			<navigator url="../applyFor/applyFor" open-type="switchTab" hover-class="navigator-hover">新用户注册</navigator>
			<navigator url="../forgotPassWord/forgotPassWord" open-type="redirect" hover-class="navigator-hover">忘记密码?</navigator>
		</view>
	</view>
</template>

<script>
	
	let test_account, test_psword; //测试用账号密码
	import eventBUs from '../../utils/eventBus.js'
	let runAnimation = true; //不知道有啥用,就放着吧
	import {
		merchantLogin,getMerchant
	} from '@/request/api.js'

	export default {
		data() {
			return {
				name: "",
				psd: "",
				grant_type: "password",
				
				psdFocus: '',
				nameFocus: '',
			}
		},
		onLoad() {
			
			
		},
		onShow() {
			const token = uni.getStorageSync('shopToken')
			if(token){
				uni.switchTab({
					url:'/pages/my/my'
				})
			}
		},
		methods: {
			onFocusPsd() {
				this.psdFocus = 'psdFocus';
			},
			onBlurPsd() {
				this.psdFocus = '';
			},
			onFocusName() {
				this.nameFocus = 'nameFocus';
			},
			onBlurName() {
				this.nameFocus = '';
			},


			login() {
				// uni.switchTab({
				// 	url: "../chat/chat"
				// });
				// return;
				runAnimation = !runAnimation
				if ( this.name == "") {
					uni.showToast({
						title:'请输入账号',
						icon: 'none',
						duration: 1500
					})
					return;
				} else if ( this.psd == "") {
					uni.showToast({
						title:'请输入密码',
						icon: 'none',
						duration: 1500
					})
					return;
				}
				// uni.setStorageSync("myUsername",test_account || this.name.toLowerCase());

				merchantLogin({account:this.name,password:this.psd}).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon:'success',
							title:res.msg
						})
						uni.setStorageSync('shopToken',res.data.userinfo.token)
						this.$eventBus.$emit('hasLogin', true);
						// console.log(res.data.userinfo)
						// uni.setStorageSync('userInfo',res.data.userinfo)
						// uni.navigateBack()
						setImmediate(async()=>{
							let res = await getMerchant();
							uni.setStorageSync('userInfo', res.data)
							uni.reLaunch({
								url: '/pages/my/my'
							})
						})
					}
				})
			}
		}
	}
	
</script>

<style>
	@import '@/common/css/pages/login.css';
</style>
